home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / TGCBOR20.ARJ / TUTOR.COM / PICKLIST.TXT < prev    next >
Text File  |  1991-08-27  |  9KB  |  351 lines

  1. PICKLISTS
  2. -----------------------------------------------------------
  3.  
  4. There are three steps to creating a picklist.
  5.  
  6.        1. Create the pick list pointer.
  7.        2. Add items to the pick list.
  8.        3. Attach the pick list to a frame.
  9.  
  10. optionmptr createpicklist(fontptr fonttype);
  11.  
  12. Creates the initial picklist pointer. Note that a picklist is built
  13. up from the same list as a menu.
  14.  
  15.  
  16. void definepickitem(optionmptr om, char *entrystr, callproc entrycallproc,
  17.                 char *pickitem);
  18.  
  19. Adds an item to the pick list.
  20.  
  21. The OM is the picklist previously created with a call to createpicklist.
  22. entrystr is the next item to be added to the pick list. entrycallproc is an
  23. event-handler to call when the item is `picked'. Note that you must define
  24. a this event-handler EVEN IF IT DOES NOTHING. Do not use nilunitproc here.
  25.  
  26. pickitem is a string where a copy of the entrystr is put. Generally all the
  27. entries for a given pick list would use the pickitem.
  28.  
  29. void definepicklistarea(imagestkptr fs, unsigned x, unsigned y, unsigned oeselect,
  30.                           unsigned displaynum, optionmptr om);
  31.  
  32. Attaches and displays a pick list on a frame.
  33.  
  34. The fs is the frame the picklist is being attached to. x,y are the coordinates
  35. of the upper left corner of the pick list. These are relative to the frame.
  36.  
  37. oeselect is the item to show as selected on entry to the picklist. If oeselect
  38. is greater that the number of entries in the picklist then no item is
  39. selected.
  40.  
  41. displaynum is the number of items to display on the picklist. displaynum must
  42. be greater than 0. 1 is a special instance for the picklist, when used the
  43. picklist is converted to a Drop-Down pick list. That is, only the selected
  44. entry is shown with a button to the right. When the button is pressed the
  45. pick list is dropped-down. If displaynum is less that the total number of
  46. picklist items then a scroll bar is displayed on the picklist, both regular
  47. and drop-down picklists.
  48.  
  49. Here is a simple program that displays a pick list.
  50.  
  51. BEGINFILE> pick1.c
  52. /* pick1.c */
  53.  
  54. #include "teglsys.h"
  55.  
  56.  
  57. char         ispicked[40];
  58.  
  59.  
  60.  
  61. unsigned waspicked( imagestkptr ifs, msclickptr ms)
  62.   {
  63.     return 1;
  64.   }
  65.  
  66.  
  67.  
  68. void         createpickframe(void)
  69.  
  70.   { optionmptr   pom;
  71.     imagestkptr  ifs;
  72.     unsigned x1 = 100;
  73.     unsigned y1 = 100;
  74.     unsigned x2 = 100;
  75.     unsigned y2 = 100;
  76.  
  77.     quickframe(&ifs,&x1,&y1,&x2,&y2);
  78.     pom = createpicklist(f8x8bold);
  79.     setpicklistmargin(pom,12);
  80.     definepickitem(pom,"Item 1",waspicked,ispicked);
  81.     definepickitem(pom,"Item 2",waspicked,ispicked);
  82.     definepickitem(pom,"Item 3",waspicked,ispicked);
  83.     definepicklistarea(ifs,10,10,2,5,pom);
  84.   }
  85.  
  86.  
  87. void main(void)
  88. {
  89.  
  90.   easytegl();
  91.   easyout();
  92.   createpickframe();
  93.  
  94.   teglsupervisor();
  95.  
  96. }
  97.  
  98.  
  99.  
  100. ENDFILE>
  101.  
  102. Of course a real program would do something with the result of the
  103. pick. It's easy to tie in the pick event to another event that will
  104. look after the result.
  105.  
  106. BEGINFILE> pick2.c
  107.   /* -- Pick2.c */
  108.  
  109. #include "teglsys.h"
  110.  
  111. char         ispicked[40];
  112.  
  113.  
  114.  
  115. unsigned waspicked( imagestkptr ifs, msclickptr ms)
  116.   { imagestkptr  locfs;
  117.  
  118.  
  119.     dropstackimage(ifs);
  120.     pushimage(100,100,200,200);
  121.     prepareforupdate(stackptr);
  122.     shadowbox(100,100,200,200);
  123.     setcolor(BLACK);
  124.     outtextxy(110,110,ispicked);
  125.     commitupdate();
  126.     return 0;
  127.   }
  128.  
  129.  
  130.  
  131. void         createpickframe(void)
  132.  
  133.   { optionmptr   pom;
  134.       imagestkptr  ifs;
  135.     unsigned x1 = 100;
  136.     unsigned y1 = 100;
  137.     unsigned x2 = 100;
  138.     unsigned y2 = 100;
  139.  
  140.  
  141.     quickframe(&ifs,&x1, &y1, &x2, &y2);
  142.     pom = createpicklist(f8x8bold);
  143.     setpicklistmargin(pom,12);
  144.     definepickitem(pom,"Item 1",waspicked,ispicked);
  145.     definepickitem(pom,"Item 2",waspicked,ispicked);
  146.     definepickitem(pom,"Item 3",waspicked,ispicked);
  147.     definepicklistarea(ifs,10,10,2,5,pom);
  148.   }
  149.  
  150.  
  151. void main(void)
  152. {
  153.  
  154.  
  155.   easytegl();
  156.   easyout();
  157.   createpickframe();
  158.  
  159.   teglsupervisor();
  160.  
  161. }
  162.  
  163.  
  164.  
  165. ENDFILE>
  166.  
  167. Here is a fairly complete and useful example of a picklist reading the
  168. directory and displaying it. It also shows how to handle it when a
  169. file is double clicked on. A more comprehensive routine would sort
  170. the filenames before sending them to the picklist and some mechanism
  171. would enable the user to move through directories.
  172.  
  173. BEGINFILE> pick3.c
  174.   /* -- pick3.c */
  175.  
  176. #include "teglsys.h"
  177. #ifdef QUICKC
  178. #include <stdlib.h>
  179. #include <direct.h>
  180. #else
  181. #include "dir.h"
  182. #endif
  183.  
  184. char         ispicked[40];   /* -- stores the picked file name  */
  185.  
  186.   /* -- drops the picklist when the frame is disposed of  */
  187.  
  188. unsigned droppicklist(imagestkptr fs,unsigned userkey,void *dataarea)
  189. {
  190.     optionmptr picklist = (optionmptr) dataarea;
  191.  
  192.     dropoptionmenu(picklist);
  193.     return 0;
  194. }
  195.  
  196.   /* -- simple, but handy  */
  197.  
  198.  
  199. void         setviewporttoframe(imagestkptr  ifs)
  200.   {
  201.     setviewport(ifs->x,ifs->y,ifs->x1,ifs->y1,TRUE);
  202.   }
  203.  
  204.   /* -- this event is called whenever a an item is clicked on  */
  205.  
  206.  
  207. unsigned waspicked(imagestkptr  ifs, msclickptr   ms)
  208.   {
  209.     setviewporttoframe(ifs);   /* -- set relative to frame  */
  210.     setfillstyle(SOLID_FILL, WHITE);   /* -- usual colors  */
  211.     bar(10,10,140,20);   /* -- bar out old file name  */
  212.     setcolor(BLACK);   /* -- black letters  */
  213.     outtextxy(10,10,ispicked);   /* -- show selection  */
  214.       /* -- left double click just checks to see if the left mouse button  */
  215.       /* -- was pressed again (within the double click delay) after entering  */
  216.       /* -- this function. If so then it means the file was selected and we  */
  217.       /* -- should exit.  */
  218.  
  219.     if (leftdoubleclick()) dropstackimage(ifs);
  220.     return 0;
  221.   }
  222.  
  223.   /* -- just tidy up the files name to make it more acceptable in the pick  */
  224.   /* -- list view.  */
  225.  
  226.  
  227. char *       fmtfilename(char * s)
  228.   {
  229.     static char    s1[21];
  230.     strcpy(s1,s);
  231.  
  232.     while (strlen(s1) < 12) strcat(s1," ");
  233.     return s1;
  234.   }
  235.  
  236.   /* -- findfirst etc. are in your TURBO manual  */
  237.  
  238.  
  239. void         getfilename(char *       path,
  240.              char *       filearg,
  241.              unsigned         attr)
  242.  
  243. {
  244. #ifdef QUICKC
  245.   struct find_t   dd;
  246. #else
  247.   struct ffblk    dd;   /* -- store directory entries  */
  248. #endif
  249.  
  250.   optionmptr   pom;
  251.   imagestkptr  ifs;
  252.   char         fullpath[80];
  253.   int          done;
  254.   unsigned x1 = 100;
  255.   unsigned y1 = 100;
  256.   unsigned x2 = 200;
  257.   unsigned y2 = 200;
  258.  
  259.     quickframe(&ifs,&x1,&y1,&x2,&y2);
  260.     setproportional(FALSE);
  261.     pom = createpicklist(f8x8bold);
  262.     setuserdataarea(stackptr,5432,pom,droppicklist);
  263.     setviewporttoframe(ifs);
  264.     setteglfont(f8x8bold);
  265.     setcolor(BLACK);
  266.     rectangle(5,5,150,23);
  267.     outtextxy(10,10,filearg);
  268.     outtextxy(10,35,path);
  269.     strcpy(fullpath,path);
  270.     strcat(fullpath,filearg);
  271. #ifdef QUICKC
  272.     done = _dos_findfirst(fullpath, _A_SUBDIR, &dd);
  273. #else
  274.     done = findfirst(fullpath,&dd,attr);
  275. #endif
  276.     while (!done)
  277.       {
  278. #ifdef QUICKC
  279.     definepickitem(pom,fmtfilename(dd.name),waspicked,ispicked);
  280. #else
  281.     definepickitem(pom,fmtfilename(dd.ff_name),waspicked,ispicked);
  282. #endif
  283.     done = findnext(&dd);
  284.       }
  285.       /* -- attach it  */
  286.     definepicklistarea(ifs,10,60,0,12,pom);
  287.  
  288.   }
  289.  
  290.  
  291. char *current_directory(char *path)
  292. {
  293.    strcpy(path, "X:\\");      /* fill string with form of response: X:\ */
  294. #ifdef QUICKC
  295.    getcwd(path,80);
  296. #else
  297.    path[0] = 'A' + getdisk();    /* replace X with current drive letter */
  298.    getcurdir(0, path+3);  /* fill rest of string with current directory */
  299. #endif
  300.    return(path);
  301. }
  302.  
  303.  
  304. char         curdir[80];
  305.  
  306. void main(void)
  307. {
  308.  
  309.  
  310.   easytegl();
  311.   easyout();
  312.                        /* -- for example just load the names  */
  313.   current_directory(curdir);
  314.   getfilename(curdir,"\\*.*",FA_ARCH);   /* -- of all the files in the current  */
  315.                      /* -- directory.  */
  316.   teglsupervisor();
  317.  
  318. }
  319.  
  320.  
  321.  
  322. ENDFILE>
  323.  
  324. void clearpicklist(optionmptr om);
  325.  
  326. Resets a pick list. All the entries are disposed of but the picklist is
  327. not. After clearpicklist is the same as having just created a picklist
  328. using createpicklist.
  329.  
  330. void setpicklistwidth(optionmptr om, unsigned maxwidth);
  331.  
  332. Set the maximum width of the picklist.
  333.  
  334. om is a pickllist previously created with createpicklist. maxwidth is the
  335. maximim width of the picklist in pixels.
  336.  
  337. void setpicklistmargin(optionmptr om, unsigned marginw);
  338.  
  339. Sets the margin around the text of the picklist.
  340.  
  341. om is a picklist previously created with createpicklist. marginw is the margin,
  342. in pixels, on the left and right side of the text.
  343.  
  344. ---------
  345. Picklists can be disposed of with a call to DropOptionMenu since they use
  346. the same data structure and list mechanism as option menus.
  347.  
  348. void dropoptionmenu(optionmptr om);
  349.  
  350. ---- END picklist.txt
  351.